package com.example.myword;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myword.dao.Word;



import java.util.List;

public class WordAdapter extends RecyclerView.Adapter<WordAdapter.WordViewHolder> {
    private List<Word> wordList;
    public WordAdapter(List<Word> wordList) {
        this.wordList = wordList;
    }

    @NonNull
    @Override
    public WordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_word, parent, false);
        return new WordViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull WordViewHolder holder, int position) {
        Word word = wordList.get(position);
        holder.textViewWord.setText(word.word);
        holder.textViewMeaning.setText("--?--");
        holder.textViewWrongCount.setText("Wrong Count: " + word.wrongCount); // 更新错误次数文本内容
        holder.textViewStatus.setText(word.known ? "Known" : "Unknown"); // 更新单词状态文本内容

        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(v.getContext(), WordActivity.class);
            intent.putExtra("word", word.word);
            intent.putExtra("meaning", word.meaning);
            intent.putExtra("userId", word.userId);
            v.getContext().startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return wordList.size();
    }

    public static class WordViewHolder extends RecyclerView.ViewHolder {
        TextView textViewWord;
        TextView textViewMeaning;
        TextView textViewWrongCount; // 添加一个用于显示错误次数的 TextView
        TextView textViewStatus; // 添加一个用于显示单词状态的 TextView

        public WordViewHolder(@NonNull View itemView) {
            super(itemView);
            textViewWord = itemView.findViewById(R.id.textViewWord);
            textViewMeaning = itemView.findViewById(R.id.textViewMeaning);
            textViewWrongCount = itemView.findViewById(R.id.textViewWrongCount); // 初始化错误次数的 TextView
            textViewStatus = itemView.findViewById(R.id.textViewStatus); // 初始化单词状态的 TextView
        }
    }
}
